package cn.hackcloud.service;

import cn.hackcloud.rpc.UserService;
import cn.hackcloud.dto.Result;
import cn.hackcloud.dto.UserVo;
import cn.hackcloud.exception.SentinelBlockHandler;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
public class UserServiceImpl implements UserService {
    static List<UserVo> userVoList = new ArrayList<>();

    static {
        userVoList.add(new UserVo(1, "赖军", 18));
        userVoList.add(new UserVo(2, "张春彤", 18));
        userVoList.add(new UserVo(3, "瞿乾浩", 18));
    }

    @Override
    @SentinelResource(value = "sentinel", blockHandler = "blockHandler", fallback = "", blockHandlerClass = SentinelBlockHandler.class)
    public Result<UserVo> get(int id) {
        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return Result.getResult(0, "OK", userVoList.stream().filter(x -> x.getId() == id).findFirst().orElse(null));
    }

    @Override
    public Result<List<UserVo>> get() {
        return Result.getResult(0, "OK", userVoList);
    }


}
